package com.hrt.manage.stock.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.hrt.base.BaseDaoImpl;
import com.hrt.bean.StockDetail;
import com.hrt.bean.Stocks;
import com.hrt.manage.stock.dao.StockDao;

@Repository
public class StockDaoImpl extends BaseDaoImpl<Stocks> implements StockDao {

	@Override
	public List<Stocks> queryByCondition(List conditions) {
		// TODO Auto-generated method stub
		return super.queryByCondition(conditions);
	}

	@Override
	public List<Stocks> queryPageByCondition(List conditions, int pageIndex,
			int pageSize) {
		// TODO Auto-generated method stub
		return super.queryPageByCondition(conditions, pageIndex, pageSize);
	}

	@Override
	public List<Stocks> getStockByType(Integer reType, Long proId) {
		List<Stocks> result = new ArrayList<Stocks>();
		String hql = "select result from Stocks result where result.reType="
				+ reType + " and result.proId=" + proId
				+ " and result.isdelete=0";
		result = this.getHibernateTemplate().find(hql);
		return result;
	}

	@Override
	public List<Stocks> getStockByType(Integer reType, Long proId,
			Integer pageIndex, Integer pageSize) {
		// TODO Auto-generated method stub
		List<Stocks> result = new ArrayList<Stocks>();
		String hql = "select result from Stocks result where result.reType="
				+ reType + " and result.proId=" + proId
				+ " and result.isdelete=0";
		result = queryByPage(hql, pageIndex, pageSize);
		return result;
	}

	@Override
	public List<Stocks> getProductByAction(Integer actStr, Integer pageIndex,
			Integer pageSize) {
		// TODO Auto-generated method stub
		List<Stocks> result = new ArrayList<Stocks>();
		String hql = "select result from Stocks result where result.action="
				+ actStr + " and result.isdelete=0";
		result = queryByPage(hql, pageIndex, pageSize);
		return result;
	}

	@Override
	public List<Stocks> getProductByReType(Integer reType, Integer pageIndex,
			Integer pageSize) {
		// TODO Auto-generated method stub
		List<Stocks> result = new ArrayList<Stocks>();
		String hql = "select result from Stocks result where result.reType="
				+ reType + "  and result.isdelete=0";
		result = queryByPage(hql, pageIndex, pageSize);
		return result;
	}

	

	@Override
	public Long totalCount(Integer reType) {
		// TODO Auto-generated method stub
		String hql="select count(s) from Stocks s where s.reType="+reType;
		List result=this.getHibernateTemplate().find(hql);
		if(result!=null)
		{
			return	new Long(result.get(0).toString());
		}
		return new Long(0);
	}

	
	@Override
	public Long saveStocks(Stocks stocks) {
		return saveEntity(stocks);
		
	}

	/**
	 * 分页查出
	 */
	@Override
	public List<Stocks> getStockList(Integer retype, Integer action,
			Integer pageIndex, Integer pageSize) {
		// TODO Auto-generated method stub
				List<Stocks> result = new ArrayList<Stocks>();
				String hql = "select result from Stocks result where result.reType="
						+ retype + " and result.action="+action+" and result.isdelete=0";
				result = queryByPage(hql, pageIndex, pageSize);

				return result;
	}

	/**
	 * 根据库单ID 取出所有库单详情
	 */
	@Override
	public List<StockDetail> getDetialsByStockId(Long stockId) {
		// TODO Auto-generated method stub
		List<StockDetail> result = new ArrayList<StockDetail>();
		String hql = "select result from StockDetail result where result.stockId="
				+ stockId + " and result.isdelete=0";
		result = this.getHibernateTemplate().find(hql);

		return result;
		
	}

	


}
